<template >
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="8">
          <el-input clearable placeholder="请输入内容" v-model="queryInfo.query" @clear="getOrdersList('search')" @keyup.enter.native="getOrdersList('search')">
            <el-button slot="append" icon="el-icon-search" @click="getOrdersList('search')"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table stripe :data="orderList" border style="width: 100%">
        <el-table-column label="#" type="index" header-align="center" align="center"></el-table-column>
        <el-table-column label="订单编号" prop="order_number" header-align="center" align="center" width="340"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" header-align="center" align="center"></el-table-column>
        <el-table-column label="是否付款" prop="order_pay" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.order_pay==='1'" effect="dark" size="mini" type="danger">未付款</el-tag>
            <el-tag v-else effect="dark" size="mini">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_send==='是'" effect="dark" size="mini">已发货</el-tag>
            <el-tag v-else effect="dark" size="mini" type="danger">未发货</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" header-align="center" align="center" width="200">
          <template slot-scope="scope">
            {{scope.row.create_time*1000 | format('yyyy-MM-dd hh:mm:ss')}}
          </template>
        </el-table-column>
        <el-table-column label="操作" header-align="center" align="center" width="120">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-tooltip enterable effect="dark" content="修改订单地址" placement="top-start">
              <el-button size="mini" type="primary" icon="el-icon-edit" @click="showEditAddressDialog"></el-button>
            </el-tooltip>
            <!-- 查看按钮 -->
            <el-tooltip enterable effect="dark" content="查看物流信息" placement="top-start">
              <el-button size="mini" type="success" icon="el-icon-location" @click="queryProgressInfo(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="pageSizes" :page-size="queryInfo.pagesize" :layout="layout" :total="total" background>
      </el-pagination>
    </el-card>

    <!-- 编辑地址的对话框 -->
    <el-dialog title="修改地址" :visible.sync="editAddressDialog" width="50%" @close="editAddressDialogClosed">
      <el-form :model="editAddressForm" :rules="editAddressFormRules" ref="editAddressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <!-- 级联选择框 -->
          <el-cascader style="width:100%" v-model="editAddressForm.address1" :options="cityData" :props="cascaderOptions"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input clearable v-model="editAddressForm.address2"></el-input>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressDialog = false">取 消</el-button>
        <el-button type="primary" @click="editAddressDialog=false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 查看物流信息的对话框 -->
    <el-dialog title="物流信息" :visible.sync="queryProgressInfoDialog" width="50%">
      <el-timeline>
        <el-timeline-item color="#0bbd87" v-for="(activity, index) in progressInfo" :key="index" :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata';
export default {
  data() {
    return {
      // 查询订单数据的参数项
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 订单总数据
      total: 0,
      // 订单数据列表
      orderList: [],
      // 分页可供选择的每页显示几条数据
      pageSizes: [5, 10, 15],
      // 分页的功能项
      layout: 'total, sizes, prev, pager, next, jumper',
      // 修改地址对话框显示隐藏控制值
      editAddressDialog: false,
      // 修改地址对话框表单数据对象
      editAddressForm: {
        address1: '',
        address2: ''
      },
      // 修改地址对话框表单数据验证规则对象
      editAddressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      // 省市区县数据  级联选择框数据配置数组
      cityData,
      // 级联选择框配置对象
      cascaderOptions: {
        expandTrigger: 'hover',
        checkStrictly: true
      },
      // 查询物流对话框显示隐藏控制值
      queryProgressInfoDialog: false,
      // 物流进度信息
      progressInfo: [
        {
          time: '2018-05-10 09:39:00',
          ftime: '2018-05-10 09:39:00',
          context: '已签收,感谢使用顺丰,期待再次为您服务',
          location: ''
        },
        {
          time: '2018-05-10 08:23:00',
          ftime: '2018-05-10 08:23:00',
          context: '[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件',
          location: ''
        },
        {
          time: '2018-05-10 07:32:00',
          ftime: '2018-05-10 07:32:00',
          context: '快件到达 [北京海淀育新小区营业点]',
          location: ''
        },
        {
          time: '2018-05-10 02:03:00',
          ftime: '2018-05-10 02:03:00',
          context: '快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]',
          location: ''
        },
        {
          time: '2018-05-09 23:05:00',
          ftime: '2018-05-09 23:05:00',
          context: '快件到达 [北京顺义集散中心]',
          location: ''
        },
        {
          time: '2018-05-09 21:21:00',
          ftime: '2018-05-09 21:21:00',
          context: '快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]',
          location: ''
        },
        {
          time: '2018-05-09 13:07:00',
          ftime: '2018-05-09 13:07:00',
          context: '顺丰速运 已收取快件',
          location: ''
        },
        {
          time: '2018-05-09 12:25:03',
          ftime: '2018-05-09 12:25:03',
          context: '卖家发货',
          location: ''
        },
        {
          time: '2018-05-09 12:22:24',
          ftime: '2018-05-09 12:22:24',
          context: '您的订单将由HLA（北京海淀区清河中街店）门店安排发货。',
          location: ''
        },
        {
          time: '2018-05-08 21:36:04',
          ftime: '2018-05-08 21:36:04',
          context: '商品已经下单',
          location: ''
        }
      ]
    }
  },
  methods: {
    // 获取订单数据列表
    async getOrdersList(keywords) {
      if (keywords === 'search') {
        this.queryInfo.pagenum = 1;
        this.queryInfo.pagesize = 5;
      }
      // console.log(this.queryInfo);
      const res = await this.$axios.get('orders', { params: this.queryInfo });
      if (res.meta.status !== 200) return this.$msg.error('获取订单数据失败！');
      this.total = res.data.total;
      this.orderList = res.data.goods;
    },

    // 每页显示的数据个数发生变化的事件函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrdersList();
    },

    // 页码值发生变化时的事件函数
    handleCurrentChange(newPagenum) {
      this.queryInfo.pagenum = newPagenum;
      this.getOrdersList();
    },

    // 显示修改地址到对话框
    showEditAddressDialog() {
      this.editAddressDialog = true;
    },

    // 修改地址对话框重置事件函数
    editAddressDialogClosed() {
      this.$refs.editAddressFormRef.resetFields();
    },

    // 查询物流信息对话框显示事件
    queryProgressInfo() {
      // this.getProgressInfo();
      this.queryProgressInfoDialog = true;
    },

    // 获取物流信息数据
    async getProgressInfo() {
      const res = await this.$axios.get('/kuaidi/75402881964554');
      if (res.meta.status !== 200) return this.$msg.error('获取物流信息数据失败！');
      this.progressInfo = res.data;
    }
  },
  created() {
    this.getOrdersList();
  }
}
</script>

<style lang="less" scoped>
</style>
